/*
 * Copyright (C) 2006 Roman Krashanitsa
 *
 * @author Roman Krashanitsa
 *
 * This file is part of paparazzi.
 *
 * paparazzi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * paparazzi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with paparazzi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA. 
 *
 */


package pprz;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.io.IOException;

import javax.comm.UnsupportedCommOperationException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import pprz.components.AcInfoContainer;
import pprz.components.FlightPlanContainer;
import pprz.components.HorizonIndicator;
import pprz.components.HwSelect;
import pprz.components.Map;
import pprz.components.MessageView;
import pprz.components.Settings;
import fr.dgac.ivy.IvyException;




public class Run {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    static private void createAndShowGUI(Link link) 
    throws IOException, UnsupportedCommOperationException, IvyException {
    	
/*    	Toolkit.getDefaultToolkit().setDynamicLayout(true);
    	try {
//    		UIManager.LookAndFeelInfo[] u=UIManager.getInstalledLookAndFeels();
 //   		System.out.println(u);
//    		UIManager.setLookAndFeel("Windows Classic");
    		//setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    	} catch (Exception e)
    	{}
*/
        JFrame frame = new JFrame("Ground station");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JTextArea alerts = new JTextArea();
		alerts.setEditable(false);
		alerts.setText("current messages");

		// Kharkov
		//Map map=new Map(49.989024,36.206887,1000);
		//Germany
		 Map map=new Map(52,5,1000, link.getAircraftList());
		//AcInfoPanel acPanel=new AcInfoPanel(link.getAircraftList());
		AcInfoContainer acPanel=new AcInfoContainer();
		Panel iPanel=new Panel();
		JPanel instruments=new JPanel();
//		instruments.setLayout(new FlowLayout());
		HorizonIndicator h=new HorizonIndicator();
		instruments.setBackground(Color.green);
		instruments.add(h);
		instruments.validate();
		FlightPlanContainer p=new FlightPlanContainer(link.getAircraftList());
		Settings s=new Settings(link.getAircraftList());
		 

		JTabbedPane lowerTabs=new JTabbedPane();
//		lowerTabs.add(h,0); lowerTabs.setTitleAt(0,"Horizon");
//		lowerTabs.add(p,1);lowerTabs.setTitleAt(1,"Flight plan"); 
//		lowerTabs.add(s,2);lowerTabs.setTitleAt(2,"Settings");
		lowerTabs.addTab("Instruments",instruments);
//		lowerTabs.addTab("Test",h);
		lowerTabs.addTab("Flight plan",p);
		lowerTabs.addTab("Settings",s);
		lowerTabs.setPreferredSize(new Dimension(300,100));
		//iPanel.setPreferredSize(new Dimension(300,100));
		//iPanel.setMinimumSize(new Dimension(300,100));
		//iPanel.setSize(new Dimension(300,100));
		 
         JSplitPane left=new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                acPanel, alerts);
         left.setDividerLocation(300);
         left.setResizeWeight(0.7);
         JSplitPane right=new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                map, lowerTabs);
         right.setDividerLocation(-250);
         right.setResizeWeight(0.7);
         JSplitPane p1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                left, right);
         p1.setDividerLocation(200);
         p1.setResizeWeight(0);
         
         frame.getContentPane().add(p1);

         
		MessageView d=new MessageView(link.getMessageFormats());
		d.setVisible(true);         
        //Link telemetry to known subscribers
        if (link!=null) 
        {
        	link.addDownlinkMessageEventListener(acPanel);
        	link.addDownlinkMessageEventListener(map);
        	link.addDownlinkMessageEventListener(h);
        	link.addDownlinkMessageEventListener(d);
        	link.addDownlinkMessageEventListener(p);
        	link.addDownlinkMessageEventListener(s);
        }
        
        //Link ac select messages
        acPanel.addAcSelectEventListener(map); 
        acPanel.addAcSelectEventListener(h);
        acPanel.addAcSelectEventListener(p);
        
        //Link map mark events
        //p.linkMap(map);
        p.addMapMarkAddRemoveEventListener(map);
        p.addMapLocationEventListener(map);
        
		Dialog dd=new HwSelect(link,frame);
		dd.setVisible(true);
         
         //Display the window.
         frame.setBounds(100, 100, 800, 600);
         frame.doLayout();
         frame.pack();
         frame.setVisible(true);
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//50635024 356625306
//		Point2D.Double p=GeographicConverter.UTMtoLL(23, 3566253.06, 506350.24, "12N");
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
          		try {
           			createAndShowGUI(new Link());
          			//createAndShowGUI(null);
          		} catch (IOException e)	{
         			System.out.println("Modem error..");
         		} catch (UnsupportedCommOperationException e)
           		{} catch (IvyException e)
           		{}
                
            }
        });
	}

}

